package com.example.rustfs.controller;

import com.example.rustfs.dto.ApiResponse;
import com.example.rustfs.dto.DeletedFileInfo;
import com.example.rustfs.service.RecycleBinService;
import com.example.rustfs.service.FileService;
import com.example.rustfs.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回收站控制器
 */
@Slf4j
@Controller
@RequestMapping("/recycle")
@RequiredArgsConstructor
public class RecycleBinController {
    
    private final RecycleBinService recycleBinService;
    private final FileService fileService;
    private final UserService userService;
    
    /**
     * 回收站页面
     */
    @GetMapping
    public String recycleBinPage(HttpSession session, Model model) {
        Long userId = (Long) session.getAttribute("userId");
        List<DeletedFileInfo> files = recycleBinService.getRecycleBinFilesInfo(userId);
        model.addAttribute("files", files);
        return "recycle";
    }
    
    /**
     * 恢复文件
     */
    @PostMapping("/restore/{id}")
    @ResponseBody
    public ApiResponse<String> restoreFile(@PathVariable Long id, HttpSession session) {
        try {
            Long userId = (Long) session.getAttribute("userId");
            recycleBinService.restoreFile(userId, id);
            
            // 更新存储空间使用量
            long storageUsed = fileService.calculateUserStorageUsed(userId);
            userService.updateStorageUsed(userId, storageUsed);
            
            return ApiResponse.success("文件恢复成功");
        } catch (Exception e) {
            log.error("恢复文件失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 永久删除
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ApiResponse<String> deletePermanently(@PathVariable Long id, HttpSession session) {
        try {
            Long userId = (Long) session.getAttribute("userId");
            recycleBinService.deletePermanently(userId, id);
            
            // 更新存储空间使用量（回收站文件不计入存储空间，但这里保持一致性）
            long storageUsed = fileService.calculateUserStorageUsed(userId);
            userService.updateStorageUsed(userId, storageUsed);
            
            return ApiResponse.success("文件已永久删除");
        } catch (Exception e) {
            log.error("永久删除失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 清空回收站
     */
    @DeleteMapping("/empty")
    @ResponseBody
    public ApiResponse<String> emptyRecycleBin(HttpSession session) {
        try {
            Long userId = (Long) session.getAttribute("userId");
            recycleBinService.emptyRecycleBin(userId);
            
            // 更新存储空间使用量
            long storageUsed = fileService.calculateUserStorageUsed(userId);
            userService.updateStorageUsed(userId, storageUsed);
            
            return ApiResponse.success("回收站已清空");
        } catch (Exception e) {
            log.error("清空回收站失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
}

